package com.atguigu.eduService.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.eduService.entity.EduChapter;
import com.atguigu.eduService.entity.EduVideo;
import com.atguigu.eduService.entity.vo.ChapterVo;
import com.atguigu.eduService.entity.vo.VideoVo;
import com.atguigu.eduService.mapper.EduChapterMapper;
import com.atguigu.eduService.service.EduChapterService;
import com.atguigu.eduService.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-11-15
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoService eduVideoService;

    //根据章节id，查询章节小节信息
    @Override

    public List<ChapterVo> getChapterVideoById(String courseId) {

        QueryWrapper<EduChapter> chapterWrapper = new QueryWrapper<>();
        chapterWrapper.eq("course_id", courseId);
        List<EduChapter> chapterList = baseMapper.selectList(chapterWrapper);

        QueryWrapper<EduVideo> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("course_id", courseId);
        List<EduVideo> videoList = eduVideoService.list(videoWrapper);


        List<ChapterVo> chapterVideoList = new ArrayList<>();
        //提取某章塞入某章的vo中
        for (int i = 0; i < chapterList.size(); i++) {
            EduChapter eduChapter = chapterList.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);
            //提取某章中的小节塞入 List<VideoVo>
            List<VideoVo> videoVo = new ArrayList<>();
            for (int j = 0; j < videoList.size(); j++) {
                EduVideo eduVideo = videoList.get(j);
                if (eduChapter.getId().equals(eduVideo.getChapterId())) {
                    VideoVo videoVos = new VideoVo();
                    BeanUtils.copyProperties(eduVideo, videoVos);
                    videoVo.add(videoVos);
                }
                chapterVo.setChildren(videoVo);
            }
            chapterVideoList.add(chapterVo);
        }
        return chapterVideoList;
    }










}



